package com.epfresh.supplier.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epfresh.api.global.BaseActivity;
import com.epfresh.supplier.R;
import com.epfresh.supplier.fragment.BaseStoreProductFragment;

public class BaseStoreSearchResultActivity extends BaseActivity {

    private View vSearch;
    private TextView tvSearch;
    private TextView etContent;
    BaseStoreProductFragment mBaseStoreProductFragment;
    String TAG_BASE_STORE_PRODUCT_FRAGMENT = "TAG_BASE_STORE_PRODUCT_FRAGMENT";
    private String mBaseStoreId;
    private boolean mHasAgent;
    private String keywords;

    @Override
    protected View getToolBar() {
        View actionbar = this.inflaterView(R.layout.toolbar_base_store_search_result, null);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(-1, this.getResources().getDimensionPixelSize(com.epfresh.api.R.dimen.action_bar_height));
        actionbar.setLayoutParams(lp);


        this.ivBack = actionbar.findViewById(R.id.iv_back);

        this.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        vSearch = actionbar.findViewById(R.id.ll_search);
        tvSearch = actionbar.findViewById(R.id.tv_search);
        etContent = actionbar.findViewById(R.id.et_content);
        etContent.setHint("请输入商品名称");

        tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onSearch();
            }
        });

        vSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onSearch();
            }
        });

        return actionbar;
    }

    private void onSearch() {


        finish();


    }


    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (savedInstanceState != null) {
            getIntent().putExtra("baseStoreId", savedInstanceState.getString("baseStoreId"));
            getIntent().putExtra("keywords", savedInstanceState.getString("keywords"));
            getIntent().putExtra("mHasAgent", savedInstanceState.getBoolean("mHasAgent",true));
        }

        mBaseStoreId = getIntent().getStringExtra("baseStoreId");
        keywords = getIntent().getStringExtra("keywords");
        mHasAgent = getIntent().getBooleanExtra("mHasAgent",true);
        setContentView(R.layout.activity_base_store_search_result);
        initView();
        etContent.setText(keywords);

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("baseStoreId", getIntent().getStringExtra("baseStoreId"));
        outState.putString("keywords", getIntent().getStringExtra("keywords"));
        outState.putString("mHasAgent", getIntent().getStringExtra("mHasAgent"));
        super.onSaveInstanceState(outState);
    }

    void initView() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        showProductInfoFragment(transaction);
        transaction.commit();
    }

    private void showProductInfoFragment(FragmentTransaction transaction) {
        if (mBaseStoreProductFragment == null) {
            mBaseStoreProductFragment = new BaseStoreProductFragment();

            transaction.add(R.id.f_product_content, mBaseStoreProductFragment, TAG_BASE_STORE_PRODUCT_FRAGMENT);
        }
        mBaseStoreProductFragment.mBaseStoreId = mBaseStoreId;
        mBaseStoreProductFragment.mHasAgent = mHasAgent;
        mBaseStoreProductFragment.keywords = keywords;
        transaction.show(mBaseStoreProductFragment);
    }

}
